package com.lyf.aop.practice.practice_5_16.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * @AUTHOR LYF
 * @DATE 2022/5/16
 * @VERSION 1.0
 * @DESC
 */
@Configuration
@EnableTransactionManagement
@PropertySource(value = "classpath:properties/datasource.properties")
public class DataSourceConfig {

    // ${}拿系统变量-配置文件；
    @Value("${user}")
    String user;
    @Value("${password}")
    String password;
    @Value("${jdbcUrl}")
    String jdbcUrl;
    @Value("${driverClassName}")
    String driverClassName;

    @Bean
    DataSource dataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        // 配置数据源特性
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl(jdbcUrl);
        dataSource.setDriverClass(driverClassName);
        System.out.println("user:"+user);
        return dataSource;
    }

    @Bean
    PlatformTransactionManager transactionManager() throws PropertyVetoException {
        return new DataSourceTransactionManager(dataSource());
    }

}
